// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/selective_struct_column_reader.h>
#include <pollux/dwio/dwrf/reader/dwrf_data.h>

namespace kumo::pollux::dwrf {

class SelectiveStructColumnReaderBase
    : public dwio::common::SelectiveStructColumnReaderBase {
 public:
  SelectiveStructColumnReaderBase(
      const TypePtr& requestedType,
      const std::shared_ptr<const dwio::common::TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec,
      bool isRoot = false)
      : dwio::common::SelectiveStructColumnReaderBase(
            requestedType,
            fileType,
            params,
            scanSpec,
            isRoot),
        rowsPerRowGroup_(formatData_->rowsPerRowGroup().value()) {
    POLLUX_CHECK_EQ(fileType_->id(), fileType->id(), "working on the same node");
  }

  void seekTo(int64_t offset, bool readsNullsOnly) override;

  void seekToRowGroup(int64_t index) override {
    dwio::common::SelectiveStructColumnReaderBase::seekToRowGroup(index);
    if (isTopLevel_ && !formatData_->hasNulls()) {
      readOffset_ = index * rowsPerRowGroup_;
      return;
    }

    // There may be a nulls stream but no other streams for the struct.
    formatData_->seekToRowGroup(index);
    // Set the read offset recursively. Do this before seeking the children
    // because list/map children will reset the offsets for their children.
    setReadOffsetRecursive(index * rowsPerRowGroup_);
    for (auto& child : children_) {
      child->seekToRowGroup(index);
    }
  }

  /// Advance field reader to the row group closest to specified offset by
  /// calling seekToRowGroup.
  void advanceFieldReader(SelectiveColumnReader* reader, int64_t offset)
      override {
    if (!reader->isTopLevel()) {
      return;
    }
    const auto rowGroup = reader->readOffset() / rowsPerRowGroup_;
    const auto nextRowGroup = offset / rowsPerRowGroup_;
    if (nextRowGroup > rowGroup) {
      reader->seekToRowGroup(nextRowGroup);
      reader->setReadOffset(nextRowGroup * rowsPerRowGroup_);
    }
  }

 private:
  const int32_t rowsPerRowGroup_;
};

class SelectiveStructColumnReader : public SelectiveStructColumnReaderBase {
 public:
  SelectiveStructColumnReader(
      const TypePtr& requestedType,
      const std::shared_ptr<const dwio::common::TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec,
      bool isRoot = false);

 private:
  void addChild(std::unique_ptr<SelectiveColumnReader> child) {
    children_.push_back(child.get());
    childrenOwned_.push_back(std::move(child));
  }

  std::vector<std::unique_ptr<SelectiveColumnReader>> childrenOwned_;
};

} // namespace kumo::pollux::dwrf
